
# 按 Shift+F10 执行或将其替换为您的代码。
# 按 双击 Shift 在所有地方搜索类、文件、工具窗口、操作和设置。
import requests
from lxml import etree
import csv

def getWeather(url):
    weather_info=[] #存放每月数据
    #请求头消息：
    headers={
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
    }
    #请求
    resp=requests.get(url,headers=headers)
    #数据预处理
    resp_html=etree.HTML(resp.text)
    #xpath提取所有数据
    resp_list=resp_html.xpath("//ul[@class='thrui']/li")
    #遍历
    for li in resp_list:
        day_weather_info={}
        #日期
        day_weather_info['date_time']=li.xpath("./div[1]/text()")[0].split(' ')[0]
        #最高气温
        high=li.xpath("./div[2]/text()")[0]
        day_weather_info['high']=high[:high.find('C')]
        #最低气温
        low=li.xpath("./div[3]/text()")[0]
        day_weather_info['low']=low[:low.find('C')]
        #天气
        day_weather_info['weather']=li.xpath("./div[4]/text()")[0]
        weather_info.append(day_weather_info)
    return weather_info

weathers=[]

#for循环生成有循序的1-12
for month in range(1,13):
    #获取某个月的天气信息
    #三元表达式
    weather_time='2022'+('0'+str(month) if month<10 else str(month))
    print(weather_time)
    url=f'https://lishi.tianqi.com/changsha/{weather_time}.html'
    #爬虫获取这个月的天气信息
    weather=getWeather(url)
    #存到列表中
    weathers.append(weather)
print(weathers)

#for循环生成有循序的1-12
for month in range(1,13):
    #获取某个月的天气信息
    #三元表达式
    weather_time='2021'+('0'+str(month) if month<10 else str(month))
    print(weather_time)
    url=f'https://lishi.tianqi.com/changsha/{weather_time}.html'
    #爬虫获取这个月的天气信息
    weather=getWeather(url)
    #存到列表中
    weathers.append(weather)
print(weathers)

#for循环生成有循序的1-12
for month in range(1,13):
    #获取某个月的天气信息
    #三元表达式
    weather_time='2020'+('0'+str(month) if month<10 else str(month))
    print(weather_time)
    url=f'https://lishi.tianqi.com/changsha/{weather_time}.html'
    #爬虫获取这个月的天气信息
    weather=getWeather(url)
    #存到列表中
    weathers.append(weather)
print(weathers)

#for循环生成有循序的1-12
for month in range(1,13):
    #获取某个月的天气信息
    #三元表达式
    weather_time='2023'+('0'+str(month) if month<10 else str(month))
    print(weather_time)
    url=f'https://lishi.tianqi.com/changsha/{weather_time}.html'
    #爬虫获取这个月的天气信息
    weather=getWeather(url)
    #存到列表中
    weathers.append(weather)
print(weathers)

#数据写入
with open("weather.csv","w",newline='')as cswfile:
    writer=csv.writer(cswfile)
    #先写入列名：columns_name
    writer.writerow(["日期","最高气温","最低气温",'天气'])
    #一次写入多行
    writer.writerows([list(day_weather_dict.values()) for month_weather in weathers for day_weather_dict in month_weather])





